package com.taxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxx.entity.SysRole;
import com.taxx.mapper.SysRoleMapper;
import com.taxx.mapper.SysRolePermissionMapper;
import com.taxx.mapper.SysRoleMenuMapper;
import com.taxx.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 系统角色服务实现类
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysRolePermissionMapper rolePermissionMapper;

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    @Override
    public List<SysRole> getRoleList() {
        return roleMapper.selectEnabledRoles();
    }

    @Override
    public SysRole getRoleById(Long id) {
        return getById(id);
    }

    @Override
    @Transactional
    public boolean createRole(SysRole role) {
        // 检查角色编码是否已存在
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_code", role.getRoleCode());
        if (count(wrapper) > 0) {
            throw new RuntimeException("角色编码已存在");
        }

        role.setStatus(1); // 默认启用
        return save(role);
    }

    @Override
    @Transactional
    public boolean updateRole(SysRole role) {
        // 检查角色编码是否已被其他角色使用
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_code", role.getRoleCode())
                .ne("id", role.getId());
        if (count(wrapper) > 0) {
            throw new RuntimeException("角色编码已存在");
        }

        return updateById(role);
    }

    @Override
    @Transactional
    public boolean deleteRole(Long id) {
        // 检查是否为超级管理员角色
        SysRole role = getById(id);
        if (role != null && "SUPER_ADMIN".equals(role.getRoleCode())) {
            throw new RuntimeException("不能删除超级管理员角色");
        }

        // 删除角色权限关联
        rolePermissionMapper.deleteByRoleId(id);
        // 删除角色菜单关联
        roleMenuMapper.deleteByRoleId(id);

        return removeById(id);
    }

    @Override
    public boolean updateRoleStatus(Long id, Integer status) {
        SysRole role = new SysRole();
        role.setId(id);
        role.setStatus(status);
        return updateById(role);
    }

    @Override
    @Transactional
    public boolean assignPermissions(Long roleId, List<Long> permissionIds) {
        // 删除原有权限关联
        rolePermissionMapper.deleteByRoleId(roleId);

        // 添加新的权限关联
        if (permissionIds != null && !permissionIds.isEmpty()) {
            return rolePermissionMapper.batchInsert(roleId, permissionIds) > 0;
        }

        return true;
    }

    @Override
    @Transactional
    public boolean assignMenus(Long roleId, List<Long> menuIds) {
        // 删除原有菜单关联
        roleMenuMapper.deleteByRoleId(roleId);

        // 添加新的菜单关联
        if (menuIds != null && !menuIds.isEmpty()) {
            return roleMenuMapper.batchInsert(roleId, menuIds) > 0;
        }

        return true;
    }

    @Override
    public List<SysRole> getRolesByUserId(Long userId) {
        return roleMapper.selectRolesByUserId(userId);
    }
}
